package com.ycy;

import cn.hutool.json.JSONUtil;
import com.corundumstudio.socketio.AckRequest;
import com.corundumstudio.socketio.Configuration;
import com.corundumstudio.socketio.SocketIOClient;
import com.corundumstudio.socketio.SocketIOServer;
import com.corundumstudio.socketio.listener.ConnectListener;
import com.corundumstudio.socketio.listener.DataListener;
import com.corundumstudio.socketio.listener.DisconnectListener;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

//import io.socket.client.Socket;

public class MySocketIOServerMain {

    static Logger logger = LoggerFactory.getLogger(MySocketIOServerMain.class);

    public static final String EVENT_ALARM_OCCUR = "ALARM_OCCUR";

    public static final String alarm_status = "ALARM_STATUS";

    public static void main(String[] args) throws InterruptedException {

        Configuration config = new Configuration();
        config.setHostname("localhost");
        config.setPort(10003);
        SocketIOServer server = new SocketIOServer(config);
        server.addConnectListener(new ConnectListener() {// 添加客户端连接监听器
            @Override
            public void onConnect(SocketIOClient client) {
                logger.info("client {}  连接到服务端了", client.getRemoteAddress());
                //发送一下试试
                client.sendEvent("helloPush", "hello");
            }
        });

        server.addDisconnectListener(new DisconnectListener() {
                                         @Override
                                         public void onDisconnect(SocketIOClient client) {
                                             logger.warn("client {} 关闭了", client.getRemoteAddress());
                                         }
                                     }
        );

        //接收
        server.addEventListener(EVENT_ALARM_OCCUR, HelloMsg.class,
                new DataListener<HelloMsg>() {
                    @Override
                    public void onData(SocketIOClient client, HelloMsg data,
                                       AckRequest ackSender) throws Exception {
                        logger.warn("client data -> {}", data);
                        logger.warn("client shake date: -> {}", client.getHandshakeData());
                        logger.warn("client context -> {}", (String)client.get("context"));

                        String context = "context";
                        boolean exists = client.has(context);
                        if(!exists) {
                            client.set(context, System.currentTimeMillis() + JSONUtil.toJsonStr(data));
                        } else {
                            String o = client.get(context);
                            client.set(context, o + System.currentTimeMillis() + JSONUtil.toJsonStr(data));

                        }


                        //发送
                        server.getBroadcastOperations().sendEvent(EVENT_ALARM_OCCUR, "hi i am server ，i get you");
                    }
                });

        server.start();

        //发送
        new Thread(new Runnable() {
            @Override
            public void run() {

                while (true) {
                    try {

                        Gson gson = new GsonBuilder().create();
                        Map<String, String> maps = new HashMap<String, String>();
                        maps.put("message_type", "place_order" + new Date());

                        server.getBroadcastOperations().sendEvent(EVENT_ALARM_OCCUR, gson.toJson(maps));

                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    try {
                        Thread.sleep(3000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }

            }
        }).start();

        Thread.sleep(Integer.MAX_VALUE);

        server.stop();
    }


}
